package framework.view.components.container.navigation
{
import com.greensock.TweenMax;

import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;

import framework.view.components.IComponent;
import framework.view.components.container.Repeater;
import framework.view.events.RepeaterEvent;

/**
 * NavigatorBase
 * @author jliang
 * 
 */
public class NavigatorBase extends Sprite implements IComponent
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function NavigatorBase()
	{
		super();
		init();
		initListener();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public var rectX:Number = 0;
	
	public var rectY:Number = 0;
	
	public var rectWidth:Number = 0;
	
	public var rectHeight:Number = 0;
	
	public var tweenTime:Number = 0;
	
	public var tweenDelay:Number = 0;
	
	public var tweenEase:Function;
	
	protected var _target:Repeater;

	public function get target():Repeater
	{
		return _target;
	}

	public function set target(value:Repeater):void
	{
		if (!value) return;
		_target = value;
		_target.addEventListener(RepeaterEvent.REPEATER_UPDATED, repeaterUpdatedHandler);
		
		setMaskRect();
	}
	
	protected var containerMask:Sprite;
	
	protected var startPoint:Point;
	//==========================================================================
	//  Public methods
	//==========================================================================	
	public function commitProperty():void
	{
		
	}
	
	public function dispose():void
	{
		if (target)
		{
			target.removeEventListener(RepeaterEvent.REPEATER_UPDATED, repeaterUpdatedHandler);
			target.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		}
		clearListener();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function init():void
	{
		
	}
	
	protected function initListener():void
	{
		
	}
	
	protected function clearListener():void
	{
		
	}
	
	protected function setMaskRect():void
	{
		if (target)
		{
			createContainerMask();
			if (target.parent)
			{
				target.parent.addChild(containerMask);
				target.mask = containerMask;
			}
			else
			{
				target.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			}
			
			/*
			var rect:Rectangle = new Rectangle(this.rectX, this.rectY, this.rectWidth, this.rectHeight);
			target.scrollRect = rect;
			*/
		}
	}
	
	protected function createContainerMask():void
	{
		if (!containerMask)
		{
			containerMask = new Sprite();
		}
		containerMask.graphics.clear();
		containerMask.graphics.beginFill(0, 0);
		containerMask.graphics.drawRect(0, 0, this.rectWidth, this.rectHeight);
		containerMask.graphics.endFill();
		containerMask.x = target.x + this.rectX;
		containerMask.y = target.y + this.rectY;
		
		startPoint = new Point(target.x, target.y);
	}
	
	protected function moveTarget(posX:Number, posY:Number):void
	{
		TweenMax.killTweensOf(target);
		TweenMax.to(target, tweenTime, { delay:tweenDelay, x:posX, y:posY, ease:tweenEase });
	}
	
	protected function reset():void
	{
		
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function repeaterUpdatedHandler(event:Event):void
	{
		reset();
	}
	
	protected function addToStageHandler(event:Event):void
	{
		target.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		target.parent.addChild(containerMask);
		target.mask = containerMask;
	}

}
}